package com.puzzlebazar.client.puzzleinfo;

import java.io.Serializable;
import java.util.Date;

import com.puzzlebazar.client.puzzle.model.History;
import com.puzzlebazar.client.util.TextUtil;


public class HistoryInfo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4677653783443967261L;

	
	private String key = null;
	private String puzzleInfoKey = null;

	private String authorEmail = "";
    private String authorUsername = "";
	private Date dateEdited = null;
	private Date dateCompleted = null;
	private History historyModel = null;

	/**
	 * @return The key for this history info model
	 */
	public String getKey() {
		return key;
	}
	
	/**
	 * @return The key for the associated puzzle info 
	 */
	public String getPuzzleInfoKey() {
		return puzzleInfoKey;
	}

	/**
	 * @return The author of this history
	 */
	public String getAuthorEmail() {
		return authorEmail;
	}
	
	/**
	 * @return The author of this history
	 */
	public String getAuthorUsername() {
		return authorUsername;
	}

	/**
	 * @return The last edition date of this history
	 */
	public Date getDateEdited() {
		return dateEdited;
	}

	/**
	 * @return The completion date of this history
	 */
	public Date getDateCompleted() {
		return dateCompleted;
	}

	/**
	 * @return The attached history model
	 */
	public History getHistoryModel() {
		return historyModel;
	}

	/**
	 * @param key The key for this history info object
	 */
	public void setKey(String key) {
		this.key = key;
		checkModelKey();
	}

	/**
	 * @return The key for the associated puzzle info 
	 */
	public void setPuzzleInfoKey(String puzzleInfoKey) {
		this.puzzleInfoKey = puzzleInfoKey;
	}
	/**
	 * @param authorUsername The author to set for that history
	 */
	public void setAuthorEmail(String authorEmail) {
		this.authorEmail = authorEmail;
	}
	/**
	 * @param authorUsername The author to set for that history
	 */
	public void setAuthorUsername(String authorUsername) {
		this.authorUsername = authorUsername;
	}
	
	/**
	 * @param dateEdited The last edition date for that history
	 */
	public void setDateEdited(Date dateEdited) {
		this.dateEdited  = dateEdited;
	}

	/**
	 * @param dateCompleted The completion date for that history
	 */
	public void setDateCompleted(Date dateCompleted) {
		this.dateCompleted  = dateCompleted;
	}

	/**
	 * @param historyModel The history model to attach to this description
	 */
	public void setHistoryModel(History historyModel) {
		this.historyModel = historyModel;
		checkModelKey();
	}

	/**
	 * Checks that the model key matches the key of this object. If it is null,
	 * it will be updated to match.
	 */
	private void checkModelKey() {
		assert( key != null );
		if( historyModel != null ) {
			String parentKey = historyModel.getHistoryInfoKey();
			if( parentKey == null )
				parentKey = key;
			else
				assert( parentKey.equals( key ) );
		}
	}
	

	/**
	 * @return True if the history is completed, false otherwise
	 */
	public boolean isCompleted() {
		return dateCompleted != null;
	}
	

	/**
	 * Formats the author username of the puzzle in a string
	 * 
	 * @param useHTML True to generate HTML markup in the string
	 * @return The formatted string
	 */
	public String getAuthorUsername( boolean useHTML ) {
		return TextUtil.putInSpan(authorUsername, "history-author", useHTML);
	}
}
